package sys

import (
	"context"
	"fmt"
	"gitee.com/freelifer/module-admin/model"
	"gitee.com/freelifer/module-admin/services/sys"
	"gitee.com/freelifer/module-web-base/request"
	"github.com/go-spring/spring-base/util"
	"github.com/go-spring/spring-core/gs"
	"github.com/go-spring/spring-core/web"
)

var ()

func init() {
	// SpringBoot.RegisterBean(new(Service)).AsInterface((*types.FileProvider)(nil))

	gs.Object(new(RoleController)).Init(func(c *RoleController) {

		///sys/admin/roles
		gs.GetMapping("/sys/admin/roles", c.GetRoles)

		// 系统角色的创建
		gs.PostBinding("/sys/admin/role", c.CreateRole)
		//SpringBoot.GetMapping("/api/user", c.GetUser)
	})
}

type RoleController struct {
	RoleService *sys.RoleService `autowire:""`
}

// 用户用户信息
func (c *RoleController) GetRoles(webCtx web.Context) {
	var info request.PageInfo
	qString := webCtx.QueryString()
	err := webCtx.Bind(&info)
	if err != nil {
		webCtx.String("参数错误")
		return
	}

	fmt.Printf("PageInfo qString %s\n", qString)
	fmt.Printf("PageInfo %d %d\n", info.Page, info.PageSize)
	_, list, _ := c.RoleService.GetRoleInfoList(info)

	webCtx.JSON(list)
}

func (c *RoleController) CreateRole(ctx context.Context, api *model.SysRole) interface{} {
	err := request.Verify(*api, ApiVerify)
	util.Panic(err).When(err != nil)

	var r model.SysRole
	err, r = c.RoleService.CreateRole(*api)
	util.Panic(err).When(err != nil)

	return r
}

func (c *RoleController) PostUser(ctx context.Context, req *userBind) string {
	return ""
}
